﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.BOM
{
    public partial class BOMChangeList : AuthBasePage
    {
        int m_count = 0;
        public int IPageIndex
        {
            get
            {
                if (null == Session["BOMIPageIndex"])
                {
                    return 0;
                }
                else
                {
                    return (int)Session["BOMIPageIndex"];
                }
            }
            set
            {
                Session["BOMIPageIndex"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData(0);
            }
        }

        private void BindData(int pageIndex)
        {
            pageIndex = IPageIndex;
            DataTable dt = BsBOMChangeBusiness.SelectList(txtBOMChangeID.Text.Trim(), txtProductName.Text.Trim(), cpFrom.Value.Trim(), cpEnd.Value.Trim(), pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
                UIHelper.GroupRows(grdResult, "lblBOMChangeID", new int[] { 0, 1, 2, 3, 9, 10, 11, 12 });
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCode = e.CommandArgument.ToString();
            string[] strs = strCode.Split(',');
            switch (e.CommandName)
            {
                case "viewdata":
                    Response.Redirect(string.Format("BOMChangeView.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "edi":
                    Response.Redirect(string.Format("BOMChangeEdit.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "del":
                    BsBOMChangeBusiness.Del(strCode);
                    BindData(0);
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    break;
                case "check":
                    BsBOMChangeBusiness.UpdateBomChangeNum(strCode);
                    BindData(0);
                    UIHelper.AlertMessage(this.Page, "审核成功");
                    break;
                default:
                    break;
            }
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }

                ImageButton ibtnCheck = (ImageButton)e.Row.FindControl("ibtnCheck");
                Button btnCheck = (Button)e.Row.FindControl("btnCheck");
                if (ibtnCheck != null)
                {
                    ibtnCheck.OnClientClick = "ShowTopConfirm('提示信息','确认是否审核？','confirmCallback(\\'" + btnCheck.ClientID + "\\')');return false;";
                }
                string status = ((DataTable)((GridView)sender).DataSource).Rows[e.Row.RowIndex]["Status"].ToString().Trim();
                if (status != "1")
                {
                    ibtnCheck.Visible = true;
                }
                else
                {
                    ibtnCheck.Visible = false;
                }
            }
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            IPageIndex = e.PageIndex;
            BindData(e.PageIndex);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }
    }
}